﻿var reg_EMail = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
var reg_QQ = /[1-9][0-9]{4,}/;
var reg_Post = /[1-9]\d{5}(?!\d)/;
var reg_IDCard = /\d{15}|\d{18}/;
var reg_UID = /^[a-zA-Z][a-zA-Z0-9_]{4,15}$/;
var reg_CN = /[\u4e00-\u9fa5]/;
var reg_Tel = /^\d{3,4}-\d{7,8}(-\d{3,4})?$/;
var reg_Mobile = /^((\(\d{3}\))|(\d{3}\-))?13[0-9]\d{8}|15[89]\d{8}/;
var reg_Number = /^\\d+$/;
var reg_URI = "([/w-]+/.)+[/w-]+.([^a-z])(/[/w-: ./?%&=]*)?|[a-zA-Z/-/.][/w-]+.([^a-z])(/[/w-: ./?%&=]*)?";

function CheckInputByID(id) {
    return CheckInput($("#" + id));
}

function CheckInput(cb) {
    var emp = cb.attr("emp"); //设置则不可为空！
    var unq = cb.attr("unq"); //设置则必须唯一
    var exp = cb.attr("exp"); //正则表达式
    var id = cb.attr("id");
    var des = cb.attr("des"); //描述
    var name = cb.attr("name");
    var disabled = cb.attr("disabled");
    var val = cb.val();
    var err = null;
    debugger;
    if (CheckNotNull(id))
        err = $("#error" + id);
    if (CheckNull(val) && CheckNotNull(emp) && CheckNotNull(err) && CheckNotNull(des)) {
        err.html('<span class="error">' + des + '不能为空.</span>');
        if (!async)
            cb.focus();
        return false;
    }
    if (CheckNotNull(val) && CheckNotNull(exp)) {
        var reg;
        if (exp.indexOf("reg_") >= 0) {
            reg = new RegExp(eval(exp));
        }
        else {
            if (exp.indexOf("Compare_") >= 0) {
                var cid = exp.replace('Compare_', '');
                if (val != $("#" + cid).val()) {
                    if (CheckNotNull(err) && CheckNotNull(des)) {
                        err.html('<span class="error">' + des + '不正确,两次输入不一致.</span>');
                    }
                    if (!async)
                        cb.focus();
                    return false;
                }
                if (CheckNotNull(err)) {
                    err.html('');
                    return true;
                }
            }
            else {
                reg = new RegExp(exp);
            }
        }
        if (!reg.test(val) && CheckNotNull(err) && CheckNotNull(des)) {
            err.html('<span class="error">' + des + '不正确.</span>');
            if (!async)
                cb.focus();
            return false;
        }
    }

    if (CheckIsAdd(disabled) && CheckNotNull(unq)) {
        var r = CheckField(id, name, val, function (re) {
            var res = CheckUnQ(re, err, val);
            if (!res && !async)
                cb.focus();
            return res;
        });
        var resu = CheckUnQ(r, err, val);
        if (!resu && !async)
            cb.focus();
        return resu;

    }
    if (CheckNotNull(err)) {
        err.html('');
        return true;
    }
}

function CheckIsAdd(disabled) {
    if (isAdd && CheckNull(disabled)) {
        return true;
    }
    else
        return false;
}

function CheckUnQ(re, err, val) {
    if (re && typeof err != 'undefined') {
        err.html('<span class="error">' + val + '已经存在.</span>');
        return false;
    }
    else {
        err.html('');
        return true;
    }
}

function CheckField(id, name, value, callBack) {
    var result = false;
    var postData = "{id:'" + id + "',name:'" + name + "',value:'" + value + "'}";
    var url = vRoot + "Services/WebService.asmx/CheckField";
    $.ajax({
        url: url,
        type: "POST",
        data: postData,
        contentType: "application/json; charset=UTF-8",
        dataType: "json",
        async: async,
        success: function (data) {
            result = data.d;
            if (async)
                callBack(data.d);
        },
        error: function (f) {
            result = false;
        }
    });
    return result;
}